<?php

namespace App\Service\Wechat;

use App\Service\WeChat;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Material extends Model
{

    const IMAGE = 'image';
    const VOICE = 'voice';
    const VIDEO = 'video';
    const THUMB = 'thumb';
    const NEWS  =   'news';

    private $wechat;
    protected $table = 'wx_material';
    public function __construct (array $attributes = [])
    {
        parent::__construct($attributes);
        $this->wechat = new WeChat();
    }

    /**
     * @return array
     */
    public static function getTypeList(){
        return [
            self::IMAGE =>  '图片素材',
            //self::VOICE =>  '音频素材',
            //self::VIDEO =>  '视频素材',
            //self::THUMB =>  '封面素材',
            self::NEWS  =>  '文章素材'
        ];
    }


    /**
     * @param $value
     *
     * @return mixed
     */
    public static function getTypeString($value){
        $config =  self::getTypeList();
        return $config[$value];
    }


    /**
     * @param Request $request
     *
     * @return $this|\Illuminate\Database\Eloquent\Builder|static
     */
    public function getPageList(Request $request){
        $this->request = $request;
        $model = $this;
        //状态
        if(isset($this->request->type)&&$this->request->type!=''){
            $model=$model->where('type','=',$this->request->type);
        }
        $model=$model->paginate($this->request->limit)
            ->toArray();
        foreach ($model['data'] as $k=>&$v){
            $v['type_name'] = $this->getTypeString($v['type']);
        }
        return $model;
    }

    /**
     * @param array $id
     */
    public function del($id = []){
        if($id == []){

        }else{
            if(is_array($id)){
                $this->whereIn('id',$id)->delete();
            }else{
                $this->where('id',$id)->delete();

            }
        }
    }

    public function sync(){
        $this->wechat->test();
    }
    public function test(){
        $this->wechat->t();
    }
    public static function getAll($type){
        switch ($type){
            case self::IMAGE:
                return MaterialImage::getAll();
                break;
            case self::NEWS:
                return MaterialNews::getALl();
                break;
        }
    }

    public static function upload($type,$data){
        switch ($type){
            case self::IMAGE:
                return MaterialImage::upload($data);
                break;
            case self::NEWS:
                return MaterialNews::upload($data);
                break;
        }
    }


}
